import Troop, random, Defines, pygame

class Mission:
	def __init__(self):
		self.name = "Steve"
		self.desc = "This is a mission"
		self.health = random.randint(90, 110)
		self.armour = random.randint(0, 2)
		self.damage = random.randint(9, 11)
		self.money = random.randint(50, 150)
		self.xp = random.randint(50, 150)
		self.difficulty = 1 # default to medium difficulty
		self.difficultyChosen = False

	def attempt(self, troopList):
		livingTroops = len(troopList)
		while livingTroops > 0 and self.health > 0: # while there are troops left and mission not complete
			for i in range (0, len(troopList)):
				if troopList[i].injured == False: # if troop is injured miss combat
					if random.randint(0, 100) >= troopList[i].evasion:# evasion chance
						troopList[i].health -= (self.damage - troopList[i].armour)

					if troopList[i].troopType == Defines.TroopType.MEDIC: # heals other troops every turn
						for j in range (len(troopList)):
							troopList[j].health += troopList[i].damage
							if troopList[j].health > 0: # revives injured troop
								troopList[j].injured = false
								livingTroops += 1
					else:
						if random.randint(0, 100) <= troopList[i].accuracy: # hit chance
							self.health -= (troopList[i].damage - self.armour)
					if troopList[i].health <= 0: # injures troop when health is empty
						troopList[i].injured = True
						livingTroops -= 1
		return troopList



	def difficulty_choice(self):
		# handles choice of difficulty and modifies mission attributes based on choice
		# user inputs a choice for difficulty here
		for event in pygame.event.get(): # event handling loop
			if event.type == KEYDOWN:
				if (event.key == K_1):
					self.difficulty = 0.8
					self.difficultyChosen = True
				elif (event.key == K_2):
					self.difficulty = 1
					self.difficultyChosen = True
				elif (event.key == K_3):
					self.difficulty = 1.2
					self.difficultyChosen = True
				elif (event.key == K_4):
					self.difficulty = 2
					self.difficultyChosen = True
				else:
					return false

		# modifies mission attributes depending on difficulty_choice
		self.health *= self.difficulty
		self.armour += self.difficulty
		self.damage *= self.difficulty
		self.money *= self.difficulty
		self.xp *= self.difficulty
		return True